package com.baijiayun.liveuibase.base

import android.content.pm.ActivityInfo
import android.os.Bundle
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
import androidx.appcompat.app.AppCompatActivity
import android.text.TextUtils
import android.widget.Toast
import com.baijiayun.livecore.utils.ToastCompat
import com.baijiayun.liveuibase.utils.DisplayUtils

/**
 * Created by pengwenjian on 2020/11/14
 * Describe:
 */
abstract class BaseScreenActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (isDefaultOrientation()) {
            initScreenOrientation()
        }
        setContentView(getLayoutId())
    }

    open fun isDefaultOrientation() = true

    private fun initScreenOrientation() {
        requestedOrientation = if (DisplayUtils.isPad(this)) {
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        } else {
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
    }

    protected open fun addFragment(layoutId: Int, fragment: androidx.fragment.app.Fragment?, addToBackStack: Boolean, fragmentTag: String?) {
        val transaction = supportFragmentManager.beginTransaction()
        if (fragmentTag == null) {
            transaction.add(layoutId, fragment!!)
        } else {
            transaction.add(layoutId, fragment!!, fragmentTag)
        }
        transaction.commitAllowingStateLoss()
    }

    protected open fun addFragment(layoutId: Int, fragment: androidx.fragment.app.Fragment?, addToBackStack: Boolean) {
        addFragment(layoutId, fragment, addToBackStack, null)
    }

    protected open fun addFragment(layoutId: Int, fragment: androidx.fragment.app.Fragment?, tag: String?) {
        addFragment(layoutId, fragment, false, tag)
    }

    protected open fun addFragment(layoutId: Int, fragment: androidx.fragment.app.Fragment?) {
        addFragment(layoutId, fragment, false)
    }

    protected open fun findFragment(layoutId: Int): androidx.fragment.app.Fragment? {
        val fm = supportFragmentManager
        return fm.findFragmentById(layoutId)
    }

    protected open fun removeFragment(fragment: androidx.fragment.app.Fragment?) {
        if (fragment == null) return
        val transaction = supportFragmentManager.beginTransaction()
        transaction.remove(fragment)
        transaction.commitAllowingStateLoss()
    }

    protected open fun removeAllFragment() {
        for (fragment in supportFragmentManager.fragments) {
            if (fragment != null) supportFragmentManager.beginTransaction().remove(fragment).commitAllowingStateLoss()
        }
    }

    protected open fun hideFragment(fragment: androidx.fragment.app.Fragment) {
        if (!fragment.isAdded) return
        val transaction = supportFragmentManager.beginTransaction()
        transaction.hide(fragment)
        transaction.commitAllowingStateLoss()
    }

    protected open fun replaceFragment(layoutId: Int, fragment: androidx.fragment.app.Fragment?) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(layoutId, fragment!!)
        transaction.commitAllowingStateLoss()
    }

    protected open fun showFragment(fragment: androidx.fragment.app.Fragment?) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.show(fragment!!)
        transaction.commitAllowingStateLoss()
    }

    protected open fun switchFragment(from: androidx.fragment.app.Fragment, to: androidx.fragment.app.Fragment, layoutId: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        if (!to.isAdded) {    // 先判断是否被add过
            transaction.hide(from).add(layoutId, to).commit() // 隐藏当前的fragment，add下一个到Activity中
        } else {
            transaction.hide(from).show(to).commit() // 隐藏当前的fragment，显示下一个
        }
    }

    protected open fun showToastMessage(message: String?) {
        if (TextUtils.isEmpty(message)) return
        runOnUiThread {
            if (this.isFinishing || this.isDestroyed) return@runOnUiThread
            ToastCompat.showToastCenter(this, message, Toast.LENGTH_SHORT)
        }
    }

    protected open fun showToastMessage(@StringRes strRes: Int) {
        showToastMessage(resources.getString(strRes))
    }

    abstract fun getLayoutId(): Int
}